package com.jams.goodox.activity.mine.question;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.library.image.ImagePagerActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/17.
 */

public class MineQuestionDetailActivity extends BaseActivity {
    @Bind(R.id.title)
    EditText title;
    @Bind(R.id.content)
    EditText content;
    @Bind(R.id.imageView)
    SimpleDraweeView imageView;
    @Bind(R.id.submm_btn)
    TextView submmBtn;

    String url;

    @Override
    protected int getLayoutId() {
        return R.layout.question_detail_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this,"查看回复");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        title.setEnabled(false);
        content.setEnabled(false);
        submmBtn.setVisibility(View.GONE);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> imageListNew = new ArrayList<>();
                imageListNew.add(url);
                Intent intent = new Intent(MineQuestionDetailActivity.this,ImagePagerActivity.class);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) imageListNew);
                startActivity(intent);
            }
        });


        A a=new A();
        a.questionId=getIntent().getIntExtra("questionId",0);
        if(a.questionId!=0){
            enqueue(createCallService(MineService.class).queueQuestionDetail(ProtocolManagers.getRequestMap(a)), new RetrofitCallbackListener<BaseEntity>() {
                @Override
                public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                    if (TextUtils.isEmpty(result.getResponseData())) return;
                    try{
                        QuesDetail quesDetail= new Gson().fromJson(result.getResponseData(),QuesDetail.class);
                        if (quesDetail!=null){
                            url = quesDetail.getImageList().get(0);
                            imageView.setImageURI(Uri.parse(url));
                            title.setText(quesDetail.getQuestion().getTitle());
                            content.setText(quesDetail.getQuestion().getDescr());
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    private static class A{
        public int questionId;
    }

}
